package com.nuanshui.heatedloan.dubbo;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class DubboResponse  implements Serializable{
	
	
	private static final long serialVersionUID = -230734691351521561L;

	private int code = 200;//200 表示成功
	
	private String msg  = "";//code 的描述信息如果 code 为200 则为空字符串

	private String dataType;//结果类型
	
	private String data ;//返回结果

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public void setMsg(String msg, int code){
		this.msg = msg;
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		if(data instanceof String) {
			this.data = (String)data;
		}else {
			this.data = JSON.toJSONString(data);
			this.dataType=data.getClass().getName();
		}	
	}
	
    public boolean isSuccess() {
    	return code == 200;
    }
    
    public Integer asInt() {
    	return Integer.valueOf(data);
    }
    
    public  boolean asBoolean() {
    	return  Boolean.valueOf(data);
    }
    
    public Long asLong() {
    	return Long.valueOf(data);
    }
    
    public String asString() {
    	return data;
    }
    public <T extends Object> T asObject(Class<T> clazz) {
    	
    	return JSON.parseObject(this.data,clazz);
    	
    }
    
    public <T extends Object> List<T> asList(Class<T> clazz) {
    	
    	return JSON.parseArray(this.data, clazz); 	
    }
    
    public Map<String, Object> asMap() {
    	return JSON.parseObject(this.data);
    }


	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public <T> T convertToObject()
	{
		T obj= null;
		try {
			if(code==DubboResponseCode.SUCCESS)
			{
				obj = (T) JSON.parseObject(getData().toString(),Class.forName(getDataType()));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return obj;
	}
}
